﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace 桌面视频录制
{
    [Serializable]
    public class DrawPolygon : DrawObject
    {
        protected  List<Point> pointList = new List<Point>();

        public List<Point> PointList
        {
            get { return pointList; }
            set { pointList = value; }
        }
        protected bool startDrawPolygon;

        public bool StartDrawPolygon
        {
            set { startDrawPolygon = value; }
        }
        protected int selectedIndex;
        public DrawPolygon(Point startPoint)
        {
            this.PointList.Add(startPoint);
        }
        public override void ChangeSize(System.Windows.Forms.MouseEventArgs e)
        {
            this.pointList[selectedIndex] = new Point(e.X, e.Y);
        }
        public override void Draw(Graphics g)
        {
            if (startDrawPolygon == false)
            {
                if (pointList.Count > 1)
                {
                    Pen pen = new Pen(this.PenColor);
                    pen.Width = this.PenWidth;
                    g.DrawLines(pen, pointList.ToArray());
                    pen.Dispose();
                }
            }
            else
            {
                Pen pen = new Pen(this.PenColor);
                pen.Width = this.PenWidth;
                g.DrawPolygon(pen, pointList.ToArray());
                pen.Dispose();
            } 
        }
        public override void DrawControlPoint(Graphics g)
        {
            foreach (Point point in pointList)
            {
                StaticFunction.DrawControlRegion(g, point);
            }
        }
        public override bool JudgeIsInControlRegion(System.Windows.Forms.MouseEventArgs e)
        {
            Rectangle rect = Rectangle.Empty;
            for (int i = 0; i < pointList.Count; i++)
			{
                rect = StaticFunction.GetControlRect(pointList[i]);
                if (rect.Contains(e.Location))
                {
                    selectedIndex = i;
                    return true;
                }
			}
            return false;
        }
        public override bool JudgeIsSelected(System.Windows.Forms.MouseEventArgs e)
        {
            GraphicsPath path = new GraphicsPath();
            path.AddPolygon(pointList.ToArray());
            Region region = new Region(path);
            if (region.IsVisible(e.Location))
            {
                path.Dispose();
                region.Dispose();
                return true;
            }
            else
            {
                path.Dispose();
                region.Dispose();
                return false;
            }
        }
        public override void MouseMove(System.Windows.Forms.MouseEventArgs e)
        {
            
        }
        public override void MoveLocation(System.Windows.Forms.MouseEventArgs e)
        {
            int dx = e.X - this.MouseDownPoint.X;
            int dy = e.Y - this.MouseDownPoint.Y;
            this.MouseDownPoint = new Point(e.X, e.Y);
            for (int i = 0; i < pointList.Count; i++)
            {
                pointList[i] = new Point(pointList[i].X + dx, pointList[i].Y + dy);
            }
        }
        public override string ToString()
        {
            string printMsg = "[多边(" + this.pointList[0].X + "," + this.pointList[0].Y + ")]->[ID:" + this.Id + "]";
            return printMsg;
        }
        public override string GetDrawObjectMsg()
        {
            string drawObjectMsg = "多边->起始位置(" + this.pointList[0].X + "," + this.pointList[0].Y + "),边数:" + this.pointList.Count + "条";
            return drawObjectMsg;
        }
        public override Rectangle GetRectangle()
        {
            return new Rectangle();
        }
        public override Point[] GetPoints()
        {
            return pointList.ToArray();
        }
        public override bool IsAllInRectangle(Region region)
        {
            foreach (Point point in pointList)
            {
                if (region.IsVisible(point) == false)
                    return false;
            }
            return true;
        }
        public override DrawObject Clone(Point point)
        {
            return new DrawPolygon(point);
        }
        public override Point[] GetTextBoxLocation()
        {
            return pointList.ToArray();
        }
    }
}
